//
//  InviteFriendView.swift
//  SmartHousekeeper
//
//  Created by 张磊 on 2020/2/22.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit

class InviteFriendView: UIView {
    
    typealias takeAddressBookBlcok = () -> ()
    var takeBookBlcok : takeAddressBookBlcok?  = nil
    
    
    typealias takePhotoViewBlcok = () -> ()
    var takePhotoBlcok : takePhotoViewBlcok?  = nil
    
    typealias InviteFriendViewBlock = ()->()
    public var appointmentBlock : InviteFriendViewBlock? = nil
    
    lazy var imgLogo : UIImageView = {
        let value = UIImageView(image: UIImage(named: "icon_yqkr"))
        return value
    }()
    
    lazy var communityL : UILabel = {
        let value = UILabel(textColor: RGBColor(r: 40, g: 40, b: 40, a: 1), textFont: UIFont.boldSystemFont(ofSize: 20))
        value.text = "邀请客人进入小区预约登记"
        return value
    }()
    
    lazy var personLable : UILabel = {
        let value = UILabel(textColor: RGBColor(r: 90, g: 90, b: 90, a: 1), textFont: UIFont.systemFont(ofSize: Adapter(13)))
        value.text = "主要来访者姓名:"
        return value
    }()
    
    lazy var backTextView : UIView = {
        let value = UIView()
        value.layer.cornerRadius = Adapter(8)
        value.layer.borderWidth = 1
        value.layer.borderColor = RGBColor(r: 140, g: 140, b: 140, a: 1).cgColor
        return value
    }()
    
    lazy var nameBtn : UIButton = {
        let value = UIButton()
        value.setImage(UIImage(named: "icon_selectName"), for: .normal)
        return value
    }()
    
    lazy var nameText : UITextField = {
        let value = UITextField()
        value.placeholder = "请输入主要来访者姓名"
        value.font = UIFont.boldSystemFont(ofSize: 17)
        value.textColor = RGBColor(r: 40, g: 40, b: 40, a: 1)
        value.textAlignment = .center
        value.delegate = self as? UITextFieldDelegate
        return value
    }()
    
    lazy var dateLable : UILabel = {
        let value = UILabel(textColor: RGBColor(r: 90, g: 90, b: 90, a: 1), textFont: UIFont.systemFont(ofSize: Adapter(13)))
        value.text = "来访日期(6小时有效):"
        return value
    }()
    
    lazy var carLable : UILabel = {
        let value = UILabel(textColor: RGBColor(r: 90, g: 90, b: 90, a: 1), textFont: UIFont.systemFont(ofSize: Adapter(13)))
        value.text = "访客车辆车牌进入预约:"
        return value
    }()
    
    lazy var dateBackView : UIView = {
        let value = UIView()
        value.layer.cornerRadius = Adapter(8)
        value.layer.borderWidth = 1
        value.layer.borderColor = RGBColor(r: 140, g: 140, b: 140, a: 1).cgColor
        return value
    }()
    
    
    lazy var  dateBtn : UIButton = {
        let value = UIButton()
        let str = getCurrentDatePoint("yyyy-MM-dd")
        value.setTitle(str, for: .normal)
        value.setTitleColor(RGBColor(r: 40, g: 40, b: 40, a: 1), for: .normal)
        value.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        value.setImage(UIImage(named: "icon_bottom"), for: .normal)
        return value
    }()
    
    lazy var  timeBtn : UIButton = {
        let value = UIButton()
        let str = getCurrentDatePoint("HH:mm")
        value.setTitle(str, for: .normal)
        value.setTitleColor(RGBColor(r: 40, g: 40, b: 40, a: 1), for: .normal)
        value.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        value.setImage(UIImage(named: "icon_bottom"), for: .normal)
        return value
    }()
    
    lazy var  carNoBtn : UIButton = {
        let value = UIButton()
        value.setImage(UIImage(named: "icon_add"), for: .normal)
        value.layer.cornerRadius = Adapter(8)
        value.layer.masksToBounds = true
        value.layer.borderWidth = 1
        value.layer.borderColor = RGBColor(r: 140, g: 140, b: 140, a: 1).cgColor
        return value
    }()
    lazy var appointmentBtn : UIButton = {
        let value = UIButton(textColor: .white, textFont: UIFont.boldSystemFont(ofSize: Adapter(20)))
        value.setTitle("立即预约", for: .normal)
        value.layer.cornerRadius = Adapter(7)
        value.layer.masksToBounds = true
        value.backgroundColor =  RGBColor(r: 62, g: 134, b: 230, a: 1)
        return value
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUi()
    }
    private func setUi(){
        self.addSubview(imgLogo)
        imgLogo.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalToSuperview().offset(Adapter(15))
            make.size.equalTo(CGSize(width: Adapter(20), height: Adapter(20)))
        }
        
        self.addSubview(communityL)
        communityL.snp.makeConstraints { (make) in
            make.left.equalTo(imgLogo.snp.right).offset(Adapter(10))
            make.centerY.equalTo(imgLogo)
            make.height.equalTo(Adapter(20))
        }
        
        let line = UIView()
        line.backgroundColor = RGBColor(r: 114, g: 114, b: 114, a: 1)
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(Adapter(15))
            make.right.equalTo(self).offset(-Adapter(15))
            make.top.equalTo(communityL.snp.bottom).offset(Adapter(20))
            make.height.equalTo(Adapter(0.8))
        }
        
        self.addSubview(personLable)
        personLable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalTo(line.snp.bottom).offset(Adapter(15))
            make.height.equalTo(Adapter(20))
        }
        
        self.addSubview(backTextView)
        backTextView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.right.equalToSuperview().offset(Adapter(-30))
            make.top.equalTo(personLable.snp.bottom).offset(Adapter(10))
            make.height.equalTo(Adapter(45))
        }
        
        self.addSubview(nameBtn)
        nameBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-Adapter(30))
            make.top.equalTo(personLable.snp.bottom).offset(Adapter(10))
            make.size.equalTo(CGSize(width: Adapter(50), height: Adapter(45)))
        }
        
        self.addSubview(nameText)
        nameText.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalTo(personLable.snp.bottom).offset(Adapter(10))
            make.height.equalTo(Adapter(45))
            make.right.equalTo(nameBtn.snp.left)
        }
        
        let line1 = UIView()
        line1.backgroundColor = RGBColor(r: 103, g: 103, b: 103, a: 1)
        self.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.top.equalTo(personLable.snp.bottom).offset(Adapter(10))
            make.size.equalTo(CGSize(width: Adapter(1), height: Adapter(45)))
            make.right.equalTo(nameBtn.snp.left)
        }
        
        self.addSubview(dateLable)
        dateLable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalTo(backTextView.snp.bottom).offset(Adapter(15))
            make.height.equalTo(Adapter(20))
        }
        
        self.addSubview(dateBackView)
        dateBackView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.right.equalToSuperview().offset(Adapter(-30))
            make.top.equalTo(dateLable.snp.bottom).offset(Adapter(10))
            make.height.equalTo(Adapter(45))
        }
        
        let w = (ScreenWidth - Adapter(60)) / 5.0 * 3.0
        
        let line2 = UIView()
        line2.backgroundColor = RGBColor(r: 103, g: 103, b: 103, a: 1)
        dateBackView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.equalTo(dateLable.snp.bottom).offset(Adapter(10))
            make.size.equalTo(CGSize(width: Adapter(1), height: Adapter(45)))
            make.left.equalToSuperview().offset(w)
        }
        
        dateBackView.addSubview(dateBtn)
        dateBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalTo(line2)
            make.bottom.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        dateBackView.addSubview(timeBtn)
        timeBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.left.equalTo(line2)
            make.bottom.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.dateBtn.imagePosition(at: .right, space: Adapter(30))
            self.timeBtn.imagePosition(at: .right, space: Adapter(30))
        }
        
        self.addSubview(carLable)
        carLable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalTo(dateBackView.snp.bottom).offset(Adapter(15))
            make.height.equalTo(Adapter(20))
        }
        
        self.addSubview(carLable)
        carLable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalTo(dateBackView.snp.bottom).offset(Adapter(20))
            make.height.equalTo(Adapter(20))
        }
        
        self.addSubview(carNoBtn)
        carNoBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.top.equalTo(carLable.snp.bottom).offset(Adapter(10))
            make.size.equalTo(CGSize(width: Adapter(w), height: Adapter(60)))
        }
        
        let company = UILabel()
        company.textColor = RGBColor(r: 116, g: 116, b: 116, a: 1)
        company.font = UIFont.systemFont(ofSize: Adapter(12))
        company.text = "技术提供商:上海领美软件科技有限公司"
        self.addSubview(company)
        company.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-Adapter(130))
            make.height.equalTo(Adapter(20))
        }
        
        
        self.addSubview(appointmentBtn)
        appointmentBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(30))
            make.right.equalToSuperview().offset(-Adapter(30))
            make.top.equalTo(company.snp.bottom).offset(Adapter(15))
            make.height.equalTo(Adapter(55))
        }
        nameBtn.sce.add(.touchUpInside) { (btn) in
            self.takeBookBlcok?()
        }
        
        //立即预约
        appointmentBtn.sce.add(.touchUpInside) { (btn) in
            self.appointmentBlock?()
        }
        
        //选择图片
        carNoBtn.sce.add(.touchUpInside) { (btn) in
            self.takePhotoBlcok?()
        }
        
        //显示日期控制器
        dateBtn.sce.add(.touchUpInside) { (btn) in
           let dateP = DatePickerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight))
            ZGetkeywindows()?.addSubview(dateP)
            dateP.datePickerBlcok = { time, type in
                self.dateBtn.setTitle(time, for: .normal)

            }
            dateP.showPicker(type:.date)
        }
        //显示时间控制器
        timeBtn.sce.add(.touchUpInside) { (btn) in
            let dateP = DatePickerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight))
            dateP.datePickerBlcok = { time, type in
                self.timeBtn.setTitle(time, for: .normal)
            }
            ZGetkeywindows()?.addSubview(dateP)
                   dateP.showPicker(type:.time)
               }
        
        
       
    }
    
    
}
